package com.lenovo.lcdm.dcm.service.impl;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import com.google.common.collect.Lists;
import com.lenovo.lcdm.common.enums.CommonMsgEnum;
import com.lenovo.lcdm.common.exception.BusinessException;
import com.lenovo.lcdm.dcm.common.util.ContextUtil;
import com.lenovo.lcdm.dcm.dto.ConfigurationCategoryCDto;
import com.lenovo.lcdm.dcm.entity.DcmBusinessConfigurationCategoryC;
import com.lenovo.lcdm.dcm.enums.ConfigurationTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther xujing30
 * @Date 2023/11/10
 * @Description
 */
public class DcmBusinessConfigurationCategoryListener extends AnalysisEventListener<ConfigurationCategoryCDto> {

    private List<DcmBusinessConfigurationCategoryC> cachedDataList = Lists.newArrayList();

    public List<DcmBusinessConfigurationCategoryC> getAllData(){
        return cachedDataList;
    }

    private String errorMsg = new String();

    private int eofIndex = -1;

    private static final String line = "Line ";



    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        throw exception;
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        if(context.readSheetHolder().getSheetName().equals(ConfigurationTypeEnum.CATEGORY_C.getSheetName())) {
            if (!headMap.values().contains("Product Segment")) {
                errorMsg += "Cannot find title:Product Segment.\n";
            }
            if (!headMap.values().contains("CHARACTERISTIC")) {
                errorMsg += "Cannot find title:CHARACTERISTIC.\n";
            }
            if (!headMap.values().contains("CATEGORY")) {
                errorMsg += "Cannot find title:CATEGORY.\n";
            }
        }
    }
    @Override
    public void invoke(ConfigurationCategoryCDto data, AnalysisContext context) {
        if (context.readSheetHolder().getSheetName().equals(ConfigurationTypeEnum.CATEGORY_C.getSheetName()) && eofIndex == -1){
            if(StringUtils.isNotEmpty(data.getNbDt()) && data.getNbDt().equals("#EOF")){
                eofIndex = cachedDataList.size();
                return;

            }
            if(StringUtils.isEmpty(data.getCharacteristic())){
                errorMsg += line + (context.readRowHolder().getRowIndex()+1) + ", CHARACTERISTIC cannot be empty.\n";
            }
            if(StringUtils.isEmpty(data.getCategory())){
                errorMsg += line + (context.readRowHolder().getRowIndex()+1) + ", CATEGORY cannot be empty.\n";
            }
            if(errorMsg.length() == 0) {
                DcmBusinessConfigurationCategoryC categoryC = new DcmBusinessConfigurationCategoryC();
                BeanUtils.copyProperties(data, categoryC);
                categoryC.setCreator(ContextUtil.getCurrentUser());
                categoryC.setCreateTime(new Date());
                categoryC.setModifier(ContextUtil.getCurrentUser());
                categoryC.setModifyTime(new Date());
                cachedDataList.add(categoryC);
            }
        }
    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if(errorMsg.length() > 0) {
            throw new BusinessException(errorMsg, CommonMsgEnum.FAIL.getCode());
        }
    }

    @Override
    public boolean hasNext(AnalysisContext context) {
        return true;
    }
}
